package life.huangzhi.community.community.controller;

import life.huangzhi.community.community.dto.PaginationDTO;
import life.huangzhi.community.community.dto.StaticDTO;
import life.huangzhi.community.community.mapper.UserMapper;
import life.huangzhi.community.community.model.User;
import life.huangzhi.community.community.service.QuestionService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * 个人信息界面
 *
 * */
@Controller
public class ProfileController {

    @Resource
    private QuestionService questionService;

    @RequestMapping("/profile/{action}")
    public String profile(HttpServletRequest request,
                          @PathVariable(name = "action") String action,
                          @RequestParam(name="page",defaultValue ="1") Integer page,
                          @RequestParam(name = "size",defaultValue ="2") Integer pageSize,
                          Model model){
        User user = (User)request.getSession().getAttribute("user");
        if(user == null){
            return "redirect:/";
        }
        PaginationDTO paginationDTO = questionService.queryQuestion(user.getId(), page, pageSize);
        if("question".equals(action)){
            model.addAttribute("sectionName", StaticDTO.MY_QUESTION);
        }else if("replies".equals(action)){
            model.addAttribute("sectionName",StaticDTO.NEW_REPLIES);
        }
        model.addAttribute("section",action);
        model.addAttribute("pagination",paginationDTO);
        return "profile";
    }
}
